package com.easybenefit.UUService.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easybeanfit.UUService.commutill.UUSharedUtil;
import com.easybeanfit.UUService.config.UUConfig;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.activity.CouponsDetailActivity;
import com.easybenefit.UUService.activity.CouponsDistributedListActivity;
import com.easybenefit.UUService.model.CouponlistBean;
import com.easybenefit.UUService.model.UserInfo;
import com.easybenefit.UUService.qrcode.QrcodeActivity;
import com.easybenefit.UUService.qrcode.QrcodeDistActivity;


import java.util.List;

/**
 * Created by 46595 on 2016/6/9.
 */
public class CouponsListAdapter extends BaseAdapter {
    private List<CouponlistBean> list;
    private Context context;
    private int type;
    private UserInfo userInfo;

    /**
     * @param list
     * @param context
     * @param type    1显示派发按钮 其他值不显示
     */
    public CouponsListAdapter(List<CouponlistBean> list, Context context, int type) {
        this.list = list;
        this.context = context;
        this.type = type;
        userInfo=(UserInfo) UUSharedUtil.getObject(context, UUConfig.UU_USERINFO_NAME, UserInfo.class);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
            convertView = LayoutInflater.from(context).inflate(R.layout.item_coupons_list, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_coupon_name = (TextView) convertView.findViewById(R.id.tv_coupon_name);
            viewHolder.tv_coupon_sum = (TextView) convertView.findViewById(R.id.tv_coupon_sum);
            viewHolder.tv_type_name = (TextView) convertView.findViewById(R.id.tv_type_name);
            viewHolder.tv_item_coupons_url = (TextView) convertView.findViewById(R.id.tv_item_coupons_url);
            viewHolder.tv_isloc = (TextView) convertView.findViewById(R.id.tv_isloc);
            viewHolder.tv_coupon_shop = (TextView) convertView.findViewById(R.id.tv_coupon_shop);
            if (list.get(position).getIs_col() != null&&type!=3) {
                if (list.get(position).getIs_col().equals("1")) {//1为已入选U卡券 0为未入选
                    viewHolder.tv_isloc.setVisibility(View.VISIBLE);
                }
            }
            if (type == 1) {
                //派发的显示
                viewHolder.llt_distributed = (LinearLayout) convertView.findViewById(R.id.llt_distributed);
                viewHolder.llt_distributed.setVisibility(View.VISIBLE);
                viewHolder.btn_distributed = (TextView) convertView.findViewById(R.id.btn_distributed);
                viewHolder.btn_distributed.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CouponlistBean bean = list.get(position);
                        Intent intent = new Intent(context, QrcodeActivity.class);
                        intent.putExtra("coup_name", bean.getCoupon_name());
                        intent.putExtra("coupon_id", bean.getCoupon_id());
                        intent.putExtra("type", CouponsDistributedListActivity.class.getSimpleName());
                        context.startActivity(intent);
                    }
                });
                viewHolder.tv_type_name.setTextColor(context.getResources().getColor(R.color.grey));
                viewHolder.tv_type_name.getPaint().setFakeBoldText(false);
            }
        if(list.get(position).getMer_name()==null||userInfo.getMer_name().equals(list.get(position).getMer_name())){
        }else{
            viewHolder.tv_coupon_shop.setVisibility(View.VISIBLE);
            viewHolder.tv_coupon_shop.setText(list.get(position).getMer_name());
        }
        viewHolder.tv_coupon_name.setText(list.get(position).getCoupon_name());
        viewHolder.tv_coupon_sum.setText("有效期至："+list.get(position).getEnd_time());
        viewHolder.tv_type_name.setText(list.get(position).getType_name());
        viewHolder.tv_item_coupons_url.setText(list.get(position).getUrl());
        return convertView;
    }

    class ViewHolder {
        public TextView tv_coupon_name, tv_coupon_sum, tv_type_name, tv_item_coupons_url;
        public LinearLayout llt_distributed;
        public TextView btn_distributed;
        public TextView tv_isloc;
        public TextView tv_coupon_shop;
    }
}
